package org.infodavid.common.model.impl.criteria;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.infodavid.common.model.criteria.ISortCriteria;

/**
 * The Class SortCriteriaImpl.
 */
public class SortCriteriaImpl extends ExtendedCriteriaImpl implements ISortCriteria {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -5323875552974160397L;

	public class ColumnOrder implements IColumnOrder {
		private static final long serialVersionUID = 1390116515839632068L;
		private String column = null;
		private boolean ascending = true;

		/*
		 * (non-Javadoc)
		 * @see org.infodavid.common.model.criteria.ISortCriteria.IColumnOrder#getColumn()
		 */
		public String getColumn() {
			return column;
		}

		/*
		 * (non-Javadoc)
		 * @see org.infodavid.common.model.criteria.ISortCriteria.IColumnOrder#isAscending()
		 */
		public boolean isAscending() {
			return ascending;
		}

		/*
		 * (non-Javadoc)
		 * @see org.infodavid.common.model.criteria.ISortCriteria.IColumnOrder#setAscending(boolean)
		 */
		public void setAscending(final boolean flag) {
			ascending = flag;
		}

		/*
		 * (non-Javadoc)
		 * @see org.infodavid.common.model.criteria.ISortCriteria.IColumnOrder#setColumn(java.lang.String)
		 */
		public void setColumn(final String value) {
			column = value;
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			final StringBuilder builder = new StringBuilder();

			builder.append(super.toString());
			builder.append("[column=");
			builder.append(column);
			builder.append(", ascending=");
			builder.append(ascending);
			builder.append("]");

			return builder.toString();
		}
	}

	/** The columns ordering. */
	private final Map<String,IColumnOrder> columnsOrdering = new HashMap<String,IColumnOrder>();

	/**
	 * The Constructor.
	 */
	public SortCriteriaImpl() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.criteria.ISortCriteria#getColumnsOrdering()
	 */
	public Collection<IColumnOrder> getColumnsOrdering() {
		return columnsOrdering.values();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.criteria.ISortCriteria#addColumnOrder(java.lang.String, boolean)
	 */
	public IColumnOrder addColumnOrder(final String column, final boolean ascending) {
		final IColumnOrder result = new ColumnOrder();

		result.setColumn(column);
		result.setAscending(ascending);
		columnsOrdering.put(result.getColumn(), result);

		return result;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.criteria.ISortCriteria#removeColumnOrder(java.lang.String)
	 */
	public IColumnOrder removeColumnOrder(final String column) {
		return columnsOrdering.remove(column);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuilder builder = new StringBuilder();

		builder.append(super.toString());
		builder.append("[columnsOrdering=");
		builder.append(Arrays.toString(columnsOrdering.values().toArray()));
		builder.append("]");

		return builder.toString();
	}
}
